import { RouteRecordRaw } from "vue-router";
import { AuthRoute } from "@/types/auth/route";
import { iconifyRender } from "../common/icon";
import { getLayoutComponent, getViewComponent } from "./component";

/** 限制key-value对应值类型 */
type ComponentAction = Record<AuthRoute.RouteComponent, () => void>

/**
 * 是否存在组件
 * @param item 权限路由
 * @returns 是否存在组件
 */
function hasComponent(item: AuthRoute.Route) {
    return Boolean(item.component)
}

/**
 * 是否为单级路由
 * @param item 单级路由
 * @returns 是否为单级路由
 */
function isSingleRoute(item: AuthRoute.Route) {
    return Boolean(item.meta.sigleLayout);
}

/**
 * 是否有子路由
 * @param item 权限路由
 * @returns 是否有子路由
 */
function hasChildren(item: AuthRoute.Route) {
    return Boolean(item.children && item.children.length);
}

/**
 * 给菜单栏添加可选属性
 * @param menuItem 菜单栏
 * @param icon 图标
 * @param children 菜单栏——子菜单
 */
function addOptionalProps(menuItem: GlobalMenuOption, icon?: string, children?: GlobalMenuOption[]) {
    const item = { ...menuItem };
    if (icon) {
        Object.assign(item, { icon: iconifyRender(icon) })
    }
    if (children) {
        Object.assign(item, { children })
    }
    return item
}

/**
 * 将权限路由转换成菜单
 * @param routes 路由
 */
export function transAuthRouteToMenu(routes: AuthRoute.Route[]): GlobalMenuOption[] {
    const currentMenu: GlobalMenuOption[] = [];
    // console.log("进来了这个将权限路由转换没？");

    routes.forEach((route) => {
        const { name, path, meta } = route;
        const routeName = name as string;
        let menuChildren: GlobalMenuOption[] | undefined
        if (route.children) {
            menuChildren = transAuthRouteToMenu(route.children);
        }
        const menuItem: GlobalMenuOption = addOptionalProps(
            {
                key: routeName,
                label: meta.title,
                routeName,
                routePath: path,
            },
            meta?.icon,
            menuChildren,
        )

        currentMenu.push(menuItem)
    });

    return currentMenu
}
/**
 * 将单个权限路由转换为Vue路由
 * @param item 权限路由
 * @returns Vue路由
 */
function transSingleAuthRouteToVueRoute(item: AuthRoute.Route) {
    const resultRoute: RouteRecordRaw[] = [];

    const itemRoute = { ...item } as RouteRecordRaw;

    if (hasComponent(item)) {
        console.log("这是啥啊？？？——transSingleAuthRouteToVueRoute", item);

        // debugger
        const action: ComponentAction = {
            basic() {
                console.log("在固定路由完成后出现");

                itemRoute.component = getLayoutComponent('basic');
                console.log("所以你应该有的？", itemRoute.component);
            },
            self() {
                itemRoute.component = getViewComponent(item.name)
            },
            multi() {
                // 三级及以上路由
            },
            blank() {
                // 空页面布局
            },
        }
        try {
            /* eslint-disable */
            action[item.component!]();
        } catch (error) {
            console.error("路由解析失败", item);
        }
    }

    /** 对于单独路由——当前项目使用的 */
    if (isSingleRoute(item)) {
        // 判断有无子路由
        if (hasChildren(item)) {
            console.error("单独路由不存在子路由", item);
        }

        // 捕获无效路由
        if (item.name == 'not-found-page') {
            itemRoute.children = [{
                path: '',
                name: item.name,
                component: getViewComponent('not-found-page')
            }]
        } else {
            const layout = item.meta.sigleLayout === 'basic' ? getLayoutComponent('basic') : getLayoutComponent('blank')
            const parentRoute: RouteRecordRaw = {
                component: layout,
                redirect: item.path,
                path: '',
                children: [itemRoute],
            }

            return [parentRoute]
        }

    }

    // 处理子路由
    if (hasChildren(item)) {
        const childrenRoute = item.children!.map(child => transSingleAuthRouteToVueRoute(child)).flat()
        if (item.component === 'multi') {
            // 将多级路由提取出来变为一个
            resultRoute.push(...childrenRoute)
            delete itemRoute.children
        } else {
            itemRoute.children = childrenRoute
        }
    }

    resultRoute.push(itemRoute)

    return resultRoute
}

/**
 * 将权限路由转换成Vue路由
 * @param routes 权限路由
 * @returns 所有的多级路由都会被转换成二级路由
 */
export function transMultiAuthRoutesToVueRoute(routes: AuthRoute.Route[]) {
    return routes.map((route) => transSingleAuthRouteToVueRoute(route)).flat(1);
}